﻿/*

	The border class will build a transparent border around
	the canvas window and allow interaction there such as a
	mouseclick for a pulse to that area,
	and automove functions
	
*/


package nl.sense.canvas {
	
	import flash.display.*
	import flash.events.*
	
	import nl.sense.canvas.*
	
	public class Border extends MovieClip {
		
		public var border:int = 0
		public var borderT:int = 10
		public var borderB:int = 10
		public var borderL:int = 10
		public var borderR:int = 10

		public var borderTimer:Timer
		public var borderShape:Shape = new Shape()
		public var borderContainer:MovieClip = new MovieClip()
		
		// automove
		private var autoMoveSpeed:int = 12
		private var autoMove = false
		
		public function Border( canvas:Canvas ) {
			trace("border set on ", canvas)
			
			parent.addChild(borderContainer)
			borderContainer.addChild(borderShape)
			borderContainer.addEventListener(MouseEvent.MOUSE_OVER, doCheckBorder)
			borderContainer.addEventListener(MouseEvent.MOUSE_OUT, unCheckBorder)
			borderContainer.addEventListener(MouseEvent.CLICK, doPulse)
		}
		
		private function drawBorder() {
			borderShape.graphics.clear()
			borderShape.graphics.beginFill(0x000000, 0)
			borderShape.graphics.lineStyle(0, 0x00000, 0)
			borderShape.graphics.drawRect(winX, winY, winW, winH)
			borderShape.graphics.drawRect(winX + borderR, winY+borderT, winW-borderR-borderL, winH-borderB-borderT)
		}
		
				private function doCheckBorder(evtObj:Event) {
			borderTimer = new Timer(400, 1) 
			borderTimer.addEventListener("timerComplete", activateCheckBorder)
			borderTimer.start()
			
			function activateCheckBorder(e:Event) {
				border_cursor.visible = true
				Mouse.hide()
				addEventListener(Event.ENTER_FRAME, checkBorder)
			}
		}
		
		private function unCheckBorder(evtObj:Event) {
			border_cursor.visible = false
			Mouse.show()
			removeEventListener(Event.ENTER_FRAME, checkBorder)
			borderTimer.stop()
		}
		
		private function checkBorder(evtObj:Event) {
			
			if (stage.mouseX > winX && stage.mouseX < winW && stage.mouseY > winY && stage.mouseY < winH) {
				
				var autoMoveSpeedX = vx
				var autoMoveSpeedY = vy
				
				// ### TODO  deze moet ook nog anders :(
				if (autoMove) {
					autoMoveSpeedX = ( border - ( (stage.stageWidth/2) - Math.abs(stage.mouseX - (stage.stageWidth/2) ) ) )/ 3
					autoMoveSpeedY = ( ( borderT + borderB ) - ( (stage.stageHeight/2) - Math.abs(stage.mouseY - (stage.stageHeight/2) ) ) ) / 14
				}
				
				if (stage.mouseY < (winY + borderT) && stage.mouseX < (winX + borderL) ) { // up/left
					mousePlace = "upleft"
					Mouse.hide()
					border_cursor.rotation = -135
					border_cursor.visible = true
					move_cursor.visible = false
					
					vx = autoMoveSpeedX
					vy = autoMoveSpeedY
	
				} else if (stage.mouseY < (winY + borderT) && stage.mouseX > (winW - borderL - borderR)) { // up/right
					mousePlace = "upright"
					Mouse.hide()
					border_cursor.rotation = -45
					border_cursor.visible = true
					move_cursor.visible = false
					
					vx = -autoMoveSpeedX
					vy = autoMoveSpeedY
					

				} else if (stage.mouseY > (winH - borderB - borderT) && stage.mouseX < (winX + borderL)) { // down/left
					mousePlace = "downleft"
					Mouse.hide()
					border_cursor.rotation = 135
					border_cursor.visible = true
					move_cursor.visible = false
					
					vx = autoMoveSpeedX
					vy = -autoMoveSpeedY

				} else if (stage.mouseY > (winH - borderB - borderT) && stage.mouseX > (winW - borderL - borderR)) { // down/right 
					mousePlace = "downright"
					Mouse.hide()
					border_cursor.rotation = 45
					border_cursor.visible = true
					move_cursor.visible = false
					
					vx = -autoMoveSpeedX
					vy = -autoMoveSpeedY
					
				} else if (stage.mouseY < (winY + borderT)) { // up
					allowedObjects = 3
					mousePlace = "up"
					Mouse.hide()
					border_cursor.rotation = -90
					border_cursor.visible = true
					move_cursor.visible = false
					
					vy = autoMoveSpeedY
					
				}else if (stage.mouseY > (winH - borderB - borderT)) { // down
					mousePlace = "down"
					Mouse.hide()
					border_cursor.rotation = 90
					border_cursor.visible = true
					move_cursor.visible = false
					move_cursor.visible = false
					
					vy = -autoMoveSpeedY
					
				}else if (stage.mouseX < (winX + borderL)) { // left
					allowedObjects = 3
					mousePlace = "left"
					Mouse.hide()
					border_cursor.rotation = 180
					border_cursor.visible = true
					move_cursor.visible = false
					
					vx = autoMoveSpeedX
					
				}else if (stage.mouseX > (winW - borderL - borderR)) { // right
					mousePlace = "right"
					Mouse.hide()
					border_cursor.rotation = 0
					border_cursor.visible = true
					move_cursor.visible = false
					
					vx = -autoMoveSpeedX
					
				}else{
					mousePlace = null
					border_cursor.rotation = 0
					border_cursor.visible = false
					
					if (!move_cursor.visible) {
						Mouse.show()
					}
					
					vx = 0
					vy = 0
				}
			}else{

				mousePlace = null
				border_cursor.rotation = 0
				border_cursor.visible = false
				
				if (!move_cursor.visible) {
					Mouse.show()
				}
			}
		}
		
				
		// ## Puls
		private function doPulse(mousePlace:String) {
			
			vx = 0
			dx = 0
			vy = 0
			dy = 0
			
			switch (mousePlace) {
				case ("upleft"):  // "upleft"
					goTo(x+puls, y+puls)
					break;

				case ("downleft"): //"left"
					goTo(x+puls, y-puls)
					break;
				
				case ("upright"): //"right"
					goTo(x-puls, y+puls)
					break;

				case ("downright"): // "down"
					goTo(x-puls, y-puls)				
					break;
					
				case ("up"):  // "up"
					goTo(x, y+puls)
					break;

				case ("left"): //"left"
					goTo(x+puls, y)
					break;
				
				case ("right"): //"right"
					goTo(x-puls, y)
					break;

				case ("down"): // "down"
					goTo(x, y-puls)				
					break;
					
				default :
					// empty
					break;
			}			
		}

	}
}